package com.zwj.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.common.entity.DemandersInsideAndOutsideTheSchool;
import com.zwj.common.service.DemandersInsideAndOutsideTheSchoolService;
import com.zwj.user.entity.UserAndRole;
import com.zwj.user.entity.UserId;
import com.zwj.user.security.PasswordSecurity;
import com.zwj.user.mapper.DemandersInsideAndOutsideTheSchoolMapper;
import com.zwj.user.service.UserAndRoleService;
import com.zwj.user.service.UserIdService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
* @author 23088
* @description 针对表【demanders_inside_and_outside_the_school(校内外需求方)】的数据库操作Service实现
* @createDate 2022-10-20 23:28:03
*/
@Service("demandersService")
public class DemandersInsideAndOutsideTheSchoolServiceImpl extends ServiceImpl<DemandersInsideAndOutsideTheSchoolMapper, DemandersInsideAndOutsideTheSchool>
    implements DemandersInsideAndOutsideTheSchoolService {

    @Resource
    private UserIdService userIdService;

    @Resource
    private UserAndRoleService userAndRoleService;


    @Override
    @Transactional
    public String register(DemandersInsideAndOutsideTheSchool demanders) {

        UserId userId = new UserId(200);
        userIdService.save(userId);
        demanders.setId(userId.getUserId());

        //MD5加密
        String password = demanders.getPassword();
        String md5Password = PasswordSecurity.Md5Encoding(password);
        demanders.setPassword(md5Password);

        demanders.setRegisterTime(LocalDateTime.now());
        demanders.setUpdateTime(LocalDateTime.now());

        boolean save = this.save(demanders);

        //用户与角色绑定
        UserAndRole userAndRole = new UserAndRole();
        userAndRole.setUserId(demanders.getId());
        if("招聘".equals(demanders.getUserType()))
            userAndRole.setRoleId(6L);
        else if("房东".equals(demanders.getUserType()))
            userAndRole.setRoleId(7L);
        else userAndRole.setRoleId(8L);
        userAndRole.setCreateTime(LocalDateTime.now());

        userAndRoleService.save(userAndRole);

        if(!save)
            return "错误!";

        return "注册成功!";
    }
}




